﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using

using System;
using System.Collections.Generic;
using System.Data.OracleClient;
using System.Linq;
using System.Text;
using System.Data.Common;
using NetBay.Core.BusinessEntities;
using NetBay.Core.Log;
using NetBay.Core.Extensions;
using Microsoft.Practices.EnterpriseLibrary.Data;
using NetBay.Core.DataAccess;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using Rio.Framework.RioManager;

#endregion

namespace Rio.Doc.DataAccess
{
    public class DA_Organization
    {
        #region Enum
        /// <summary>
        /// List of DB functions to access data
        /// </summary>
        public enum DbFunction
        {
            [DescriptionAttribute("")]
            None,
            [DescriptionAttribute("P_ORGANIZATION_Get_ByGroupId")]
            GetOrganizationByGroupId,
            [DescriptionAttribute("P_ORGANIZATION_List")]
            GetAllOrganization,
            [DescriptionAttribute("P_ADMIN_Organization_Deact")]
            DeactivateOrganization,
            [DescriptionAttribute("P_ADMIN_ORGANIZATION_Add")]
            AddOrganization,
            [DescriptionAttribute("P_ADMIN_ORGANIZATION_Update")]
            UpdateOrganization,
            [DescriptionAttribute("P_ADMIN_Organization_Rank_Upd")]
            RankUpdate,
            [DescriptionAttribute("P_ORGANIZATION_List_ByUserId")]
            GetOrganizationsByUserId,
            [DescriptionAttribute("P_ORGANIZATION_Get_ByUserId")]
            GetDisctinctOrganizationsByUserId,
            [DescriptionAttribute("P_ORGANIZATION_Get_ById")]
            GetOrganizationById
        }
        #endregion

        /// <summary>
        /// Return on user
        /// </summary>
        /// <param name="p_lGroupId">The user iedntifier</param>
        /// <returns>The specfied user</returns>
        public static IDataReader GetOrganizationByGroup(long p_lGroupId)
        {
            var db = DbHelper.DataBase;

            var dbCommand = db.GetNewCommand(DbFunction.GetOrganizationByGroupId.szGetDescription());
            db.AddInParameter(dbCommand, "p_groupId", DbType.Int32, p_lGroupId);

            return db.ExecuteReader(dbCommand);
        }

        /// <summary>
        /// Returns all RIO organization.
        /// </summary>
        /// <returns>List of organization</returns>
        public static IDataReader GetAllOrganization()
        {
            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("DA_Organization.GetAllOrganization (No parameters)...");

            try
            {
                //Variable initialization.
                var db = DbHelper.DataBase;
                var dbCommand = db.GetNewCommand(DbFunction.GetAllOrganization.szGetDescription());

                return db.ExecuteReader(dbCommand);
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("DA_Organization.GetAllOrganization Exception [" + ex.Message + "]");
                return null;
            }
        }

        /// <summary>
        /// Gets the organizations by user id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        public static IDataReader GetOrganizationsByUserId(long userId)
        {
            LogManager.Instance().LogDebugFormat("Calling SP: '{0}'", DbFunction.GetOrganizationsByUserId.szGetDescription());
            LogManager.Instance().LogDebug("(p_luserId<" + userId + ">)...");

            try
            {
                var db = DbHelper.DataBase;

                var dbCommand = db.GetNewCommand(DbFunction.GetOrganizationsByUserId.szGetDescription());
                db.AddInParameter(dbCommand, "p_luserId", DbType.Int64, userId);

                return db.ExecuteReader(dbCommand);
            }
            catch (Exception ex)
            {
                //Error.
                LogManager.Instance().LogError("DA_Organization.GetOrganizationsByUserId Exception [" + ex.Message + "]");
                return null;
            }
        }

        /// <summary>
        /// Gets the organizations by user id.
        /// </summary>
        /// <param name="organizationId">The organization id.</param>
        /// <returns></returns>
        public static IDataReader GetOrganizationId(long organizationId)
        {
            LogManager.Instance().LogDebugFormat("Calling SP: '{0}'", DbFunction.GetOrganizationById.szGetDescription());
            LogManager.Instance().LogDebug("(p_organizationId<" + organizationId + ">)...");

            try
            {
                var db = DbHelper.DataBase;

                var dbCommand = db.GetNewCommand(DbFunction.GetOrganizationById.szGetDescription());
                db.AddInParameter(dbCommand, "p_organizationId", DbType.Int64, organizationId);

                return db.ExecuteReader(dbCommand);
            }
            catch (Exception ex)
            {
                //Error.
                LogManager.Instance().LogError("DA_Organization.GetOrganizationById Exception [" + ex.Message + "]");
                return null;
            }
        }

        /// <summary>
        /// Gets the organizations by user id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        public static IDataReader GetDisctinctOrganizationsByUserId(long userId)
        {
            LogManager.Instance().LogDebugFormat("Calling SP: '{0}'", DbFunction.GetDisctinctOrganizationsByUserId.szGetDescription());
            LogManager.Instance().LogDebug("(p_luserId<" + userId + ">)...");

            try
            {
                var db = DbHelper.DataBase;

                var dbCommand = db.GetNewCommand(DbFunction.GetDisctinctOrganizationsByUserId.szGetDescription());
                db.AddInParameter(dbCommand, "p_luserId", DbType.Int64, userId);

                return db.ExecuteReader(dbCommand);
            }
            catch (Exception ex)
            {
                //Error.
                LogManager.Instance().LogError("DA_Organization.GetOrganizationsByUserId Exception [" + ex.Message + "]");
                return null;
            }
        }

        /// <summary>
        /// Updates the rank organization.
        /// </summary>
        /// <param name="orgaSourceId">The orga source id.</param>
        /// <param name="orgaSourceRank">The orga source rank.</param>
        /// <param name="orgaDestinationId">The orga destination id.</param>
        /// <param name="orgaDestinationRank">The orga destination rank.</param>
        /// <returns></returns>
        public static bool UpdateRankOrganization(long orgaSourceId, int orgaSourceRank, long orgaDestinationId, int orgaDestinationRank)
        {
            bool result = false;

            LogManager.Instance().LogDebugFormat("Calling SP: '{0}'", DbFunction.RankUpdate.szGetDescription());
            LogManager.Instance().LogDebug("(p_lOrgaSource<" + orgaSourceId + "> p_lOrgaSourceRank<" + orgaSourceRank + "> p_lOrgaDestination<" + orgaDestinationId + "> p_lOrgaDestinationRank<" + orgaDestinationRank + ">)...");

            try
            {
                var db = DbHelper.DataBase;

                using (var dbCommand = db.GetNewCommand(DbFunction.RankUpdate.szGetDescription()))
                {
                    db.AddInParameter(dbCommand, "p_lOrgaSource", DbType.Int64, orgaSourceId);
                    db.AddInParameter(dbCommand, "p_lOrgaSourceRank", DbType.Int32, orgaSourceRank);
                    db.AddInParameter(dbCommand, "p_lOrgaDestination", DbType.Int64, orgaDestinationId);
                    db.AddInParameter(dbCommand, "p_lOrgaDestinationRank", DbType.Int32, orgaDestinationRank);
                    db.ExecuteNonQuery(dbCommand);
                }
                result = true;
            }
            catch (Exception ex)
            {
                //Error.
                LogManager.Instance().LogError("DA_Organization.UpdateRankOrganization Exception [" + ex.Message + "]");
            }

            return result;
        }

        /// <summary>
        /// Returns all RIO organization.
        /// </summary>
        /// <returns>List of organization</returns>
        public static IDataReader GetAllOrganization(bool withDeleted = false)
        {
            //Log.
            LogManager.Instance().LogDebugFormat("Calling SP: '{0}'", DbFunction.GetAllOrganization.szGetDescription());

            try
            {
                //Variable initialization.
                var db = DbHelper.DataBase;
                var dbCommand = db.GetNewCommand(DbFunction.GetAllOrganization.szGetDescription());
                db.AddInParameter(dbCommand, "p_lDelete", DbType.Int16, Convert.ToInt16(withDeleted));

                return db.ExecuteReader(dbCommand);
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("DA_Organization.GetAllOrganization Exception [" + ex.Message + "]");
                return null;
            }
        }

        /// <summary>
        /// Deletes the organization.
        /// </summary>
        /// <param name="identifier">The identifier.</param>
        /// <returns></returns>
        public static StoredProcedureResult DeleteOrganization(long identifier)
        {
            StoredProcedureResult result = StoredProcedureResult.GetDefault();
            LogManager.Instance().LogDebugFormat("Calling SP: '{0}'", DbFunction.DeactivateOrganization.szGetDescription());

            try
            {
                var db = DbHelper.DataBase;
                var dbCommand = db.GetNewCommand(DbFunction.DeactivateOrganization.szGetDescription());
                db.AddInParameter(dbCommand, "p_lIdentifier", DbType.Int64, identifier);
                result = db.ExecuteAndGetResult(dbCommand);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(ex.ToString());
                return new StoredProcedureResult(ResultCode.Error, ex.Message);
            }
            return result;
        }

        /// <summary>
        /// Adds the organization.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="description">The description.</param>
        /// <returns></returns>
        public static StoredProcedureResult AddOrganization(string name, string description)
        {
            StoredProcedureResult result = StoredProcedureResult.GetDefault();
            LogManager.Instance().LogDebugFormat("Calling SP: '{0}'", DbFunction.AddOrganization.szGetDescription());
            LogManager.Instance().LogDebug("(p_sName<" + name + ">(p_sDescription<" + description + ">)...");
            try
            {
                var db = DbHelper.DataBase;
                var dbCommand = db.GetNewCommand(DbFunction.AddOrganization.szGetDescription());
                db.AddInParameter(dbCommand, "p_sName", DbType.String, name);
                db.AddInParameter(dbCommand, "p_sDescription", DbType.String, description);
                result = db.ExecuteAndGetResult(dbCommand);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(ex.ToString());
                return new StoredProcedureResult(ResultCode.Error, ex.Message);
            }
            return result;
        }

        /// <summary>
        /// Updates the organization.
        /// </summary>
        /// <param name="identifier">The identifier.</param>
        /// <param name="name">The name.</param>
        /// <param name="description">The description.</param>
        /// <param name="rank">The rank.</param>
        /// <param name="isdeleted">if set to <c>true</c> [isdeleted].</param>
        /// <returns></returns>
        public static StoredProcedureResult UpdateOrganization(long identifier, string name, string description, bool isdeleted = false)
        {
            StoredProcedureResult result = StoredProcedureResult.GetDefault();
            LogManager.Instance().LogDebugFormat("Calling SP: '{0}'", DbFunction.UpdateOrganization.szGetDescription());
            LogManager.Instance().LogDebug("(p_sName<" + name + ">(p_sDescription<" + description + ">)...");
            try
            {
                var db = DbHelper.DataBase;
                var dbCommand = db.GetNewCommand(DbFunction.UpdateOrganization.szGetDescription());
                db.AddInParameter(dbCommand, "p_lIdentifier", DbType.Int64, identifier);
                db.AddInParameter(dbCommand, "p_sName", DbType.String, name);
                db.AddInParameter(dbCommand, "p_sDescription", DbType.String, description);
                db.AddInParameter(dbCommand, "p_lDeleted", DbType.Int16, Convert.ToInt16(isdeleted));
                result = db.ExecuteAndGetResult(dbCommand);

            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(ex.ToString());
                return new StoredProcedureResult(ResultCode.Error, ex.Message);
            }
            return result;
        }

    }
}
